package base.loader
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import utils.ApplicationDomainUtil;

	/**
	 *图片加载类 
	 */
	public class ImageLoader extends LoaderBase
	{
		private var _bitmapData:BitmapData;
		private var _bitmap:Bitmap=new Bitmap();
		private var _className:String;
		private var _width:int=-1;
		private var _height:int=-1;
		private var _smoothing:Boolean;
		public function get smoothing():Boolean
		{
			return _smoothing;
		}

		public function set smoothing(value:Boolean):void
		{
			_smoothing = value;
		}

		
		public function ImageLoader(loaderFlagType:String, id:int,url:String, isSharedCache:Boolean=true, repeat:int=3, loaderVersion:int=0, level:int=0)
		{
			super(loaderFlagType, id, url, isSharedCache, repeat, loaderVersion, level);
		}
		
		override protected function loaderComplete():void
		{
			_bitmapData=ApplicationDomainUtil.getDefinitionObject(_className) as BitmapData;
			_bitmap.bitmapData=_bitmapData;
			if(_width>0)_bitmap.width=_width;
			if(_height>0)_bitmap.height=_height;
			super.loaderComplete();
		}

		public function get bitmapData():BitmapData
		{
			return _bitmapData;
		}

		public function get className():String
		{
			return _className;
		}

		public function set className(value:String):void
		{
			_className = value;
		}
		
		override public function clone():BaseLoader
		{
			var imageLoader:ImageLoader=new ImageLoader(_loaderFlagType, _id, _url, _isSharedCache,_repeat, _loaderVersion);
			imageLoader.isComplete=_isComplete;
			imageLoader.isSuccess=_isSuccess;
//			imageLoader.isLoading=_isLoading;
			imageLoader.progress=_progress;
			imageLoader.className=_className;
			imageLoader.width=_width;
			imageLoader.height=_height;
			if(!_bitmapData)
			{
				_bitmapData=ApplicationDomainUtil.getDefinitionObject(_className) as BitmapData;
			}
			imageLoader.bitmap=new Bitmap(_bitmapData);
			if(_width>0)imageLoader.bitmap.width=_width;
			if(_height>0)imageLoader.bitmap.height=_height;
			imageLoader.bitmap.smoothing=_smoothing;
			return imageLoader;
		}
		
		override public function dispose():void
		{
			if(_bitmapData)
			{
				_bitmapData.dispose();
				_bitmapData=null;
			}
			if(_bitmap)
			{
				if(_bitmap.parent){
					_bitmap.parent.removeChild(_bitmap);
				}
				if(_bitmap.bitmapData)_bitmap.bitmapData.dispose();
				_bitmap.bitmapData=null;
				_bitmap=null;
			}
			super.dispose();
		}

		public function get bitmap():Bitmap
		{
			return _bitmap;
		}

		public function get width():int
		{
			return _width;
		}

		public function set width(value:int):void
		{
			_width = value;
		}

		public function get height():int
		{
			return _height;
		}

		public function set height(value:int):void
		{
			_height = value;
		}

		public function set bitmap(value:Bitmap):void
		{
			_bitmap = value;
		}
	}
}